package controller.library
{
	import events.library.EventCreateNewLibraryItem;
	
	import model.ModelLibrary;
	import model.ModelProject;
	
	import org.flashIso.engine.library.objects.LBinaryData;
	import org.flashIso.engine.library.objects.LObject;
	import org.flashIso.engine.library.objects.LImage;
	import org.flashIso.engine.library.objects.LTile;
	import org.robotlegs.mvcs.Command;
	
	import services.IServiceFile;
	
	public class CommandCreateNewLibraryItem extends Command
	{
		[Inject]
		public var event:EventCreateNewLibraryItem;
		
		[Inject]
		public var ml:ModelLibrary;
		
		[Inject]
		public var mp:ModelProject;
		
		[Inject]
		public var f:IServiceFile;
		
		override public function execute() : void {
			mp.projectModified = true;

			var id:String;
			var i:uint;
			
			switch (event.type){
				case EventCreateNewLibraryItem.CREATE_NEW_BINARY:
					var b:LBinaryData = new LBinaryData();
					b.id = mp.newItemName;
					if (event.filePath != null){
						b.data = f.readFile(event.filePath);
					}
					ml.addLBinaryData(b);
					break;
				case EventCreateNewLibraryItem.CREATE_NEW_IMAGE:
					var image:LImage = new LImage();
					image.id = event.newObjectID;
					image.data = f.readFile(event.filePath);
					ml.addLImage(image);
					break;
				case EventCreateNewLibraryItem.CREATE_NEW_OBJECT:
					id = mp.newItemName;
					var o:LObject = new LObject();
					o.id = id;
					ml.addLObject(o);
					break;
				case EventCreateNewLibraryItem.CREATE_NEW_TILE:
					id = mp.newItemName;
					var t:LTile = new LTile();
					t.id = id;
					ml.addLTile(t);
					break;
				default:
					throw(new Error("Unknown type of library item!"));
					break;
			}
		}
		

	}
}